﻿namespace EMall.Uow
{
    public abstract class UnitOfWork : IUnitOfWork
    {
        public bool IsCommitted { get; protected set; }
        public virtual void Begin() => BeginAsync().Wait();
        public abstract Task BeginAsync();
        public virtual void OnCommitExecuting() => OnCommitExecutingAsync().Wait();
        public abstract Task OnCommitExecutingAsync();
        public virtual void Commit() => CommitAsync().Wait();
        public virtual async Task CommitAsync()
        {
            if (!IsCommitted)
            {
                await OnCommitExecutingAsync();
                await OnCommitAsync();
                IsCommitted = true;
                await OnCommitExecutedAsync();
            }
        }
        public abstract Task OnCommitAsync();
        public virtual void OnCommitExecuted() => OnCommitExecutedAsync().Wait();
        public abstract Task OnCommitExecutedAsync();
        public virtual void Rollback() => RollbackAsync().Wait();
        public abstract Task RollbackAsync();
        public abstract void Dispose();
    }
}
